package com.overlord.im.server.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.overlord.im.commons.mq.MqConstants;
import com.overlord.im.commons.mq.QueueUtils;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @ClassName OverlordConfig
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/29 15:59
 **/
@Configuration
public class OverlordServerConfig {

    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MybatisMetaObjectHandler();
    }

    @Bean
    @Profile({"dev", "test", "default"})
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

    @Bean
    public Exchange imExchange() {
        return new DirectExchange(MqConstants.OVERLORD_IM_EXCHANGE, true, false);
    }

    @Bean
    public Queue msgQueue() {
        return QueueUtils.createDefaultQueue(MqConstants.OVERLORD_IM_MESSAGE_REDIRECT);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(msgQueue()).to(imExchange()).with(MqConstants.OVERLORD_IM_MESSAGE_REDIRECT).noargs();
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }
}
